package com.IClass.IActivity;


import java.util.Map;

import com.IClass.iAskListAdapter;
import com.IClass.iHotBlogListAdapter;
import com.IClass.App.IClass;
import com.IClass.App.R;
import com.IClass.image.AsynImageLoader;
import com.ant.liao.GifView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class IAskActivity extends Activity  implements OnCheckedChangeListener{
	private static final String TAG = "IAskActivity";
	private static final int SEARCH_SUCESS = 900000;
	//public TextView textview2;
	//public Button button2;
	
//	public TabHost mHost;
//	public Intent mHot;
//	public Intent mTalk;
//	public Intent mRecommend;
//	public Intent mAttention;
//	public Intent mMyself;
//	private RadioGroup mainTab;
	public TextView answertext;
	public TextView rettime;
	public ImageButton iaskioclassbtn;
	
	public Context context;
	public Handler handler = new Handler();
	//public AsynImageLoader imageloader;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.iask_layout2);
		
		rettime = ((TextView)findViewById(R.id.retimetext));
		
		
		
		
//		GifView gf1 = (GifView) findViewById(R.id.iask_gif);
//		// 设置Gif图片源
//		gf1.setGifImage(R.drawable.ioclass7);
//		
//		GifView gf2 = (GifView) findViewById(R.id.iask2_gif);
//		// 设置Gif图片源
//		gf2.setGifImage(R.drawable.ioclass13);
		
		//answertext = ((TextView)findViewById(R.id.answertext));
		
		iaskioclassbtn = ((ImageButton)findViewById(R.id.iaskioclassbtn));
		
		
		
		iaskioclassbtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// FIXME Auto-generated method stub
				  
				Message msg = new Message();
				msg.what = IAskActivity.SEARCH_SUCESS;
				handler.sendMessage(msg);
			}
			
		});
		context = this;
		
		SharedPreferences  share = context.getSharedPreferences("IClass", MODE_PRIVATE);  
        String GetHotBlogsdata = share.getString("GetHotBlogs", "");
        
        if(!GetHotBlogsdata.equals(""))
        {
        	IClass.GetHotBlogsData(GetHotBlogsdata);
        	
			iHotBlogListAdapter listItemAdapter = new iHotBlogListAdapter(context,IClass.gethotblogslist());
			ListView list = (ListView) findViewById(R.id.searchlistview);  
			
			list.setAdapter(listItemAdapter);
			
			
			list.setOnItemClickListener(new OnItemClickListener() {  
				  
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
				{
					// TODO Auto-generated method stub
					iHotBlogListAdapter t = (iHotBlogListAdapter)arg0.getAdapter();
					Map<String, Object> m = (Map<String, Object>)t.getItem(arg2);
					String blogid = (String)m.get("hash_data");
					blogid = blogid.substring(blogid.indexOf("blogid") + 6);
					
					Intent blogdisscuss = new Intent(arg0.getContext(),IBlogDiscussActivity.class);
					blogdisscuss.putExtra("blogid", blogid);
					startActivity(blogdisscuss);    //启动Main界面
	                
				}  
	        } );
        }
        else
        {
        	new Thread() {
                @Override
                public void run() {
                	long starttime = System.currentTimeMillis();
                	String result = IClass.classapi.GetHotBlogs();
                	long gettime = (System.currentTimeMillis() - starttime);
                	//Log.i(TAG, "GetFirtShowData end" + );
                	IClass.setGetGetHotBlogstime(gettime);
                	
                	Editor  share = context.getSharedPreferences("IClass", MODE_PRIVATE).edit();  
                    share.putString("GetHotBlogs", result);
                    share.commit();
                    
                    
    				handler.post(new Runnable() {
    					@Override
    					public void run() {

    						rettime.setText(String.format("(用时 %f 秒)",IClass.getGetGetHotBlogstime()/1000.0));
    						iHotBlogListAdapter listItemAdapter = new iHotBlogListAdapter(context,IClass.gethotblogslist());
    						ListView list = (ListView) findViewById(R.id.searchlistview);  
    						
    						list.setAdapter(listItemAdapter);
    						
    						
    						list.setOnItemClickListener(new OnItemClickListener() {  
    							  
    							@Override
    							public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
    							{
    								// TODO Auto-generated method stub
    								iHotBlogListAdapter t = (iHotBlogListAdapter)arg0.getAdapter();
    								Map<String, Object> m = (Map<String, Object>)t.getItem(arg2);
    								String blogid = (String)m.get("hash_data");
    								blogid = blogid.substring(blogid.indexOf("blogid") + 6);
    								
    								Intent blogdisscuss = new Intent(arg0.getContext(),IBlogDiscussActivity.class);
    								blogdisscuss.putExtra("blogid", blogid);
    								startActivity(blogdisscuss);    //启动Main界面
    				                
    							}  
    				        } );
    					}

    				});
    				
    				
                }
            }.start();
        }
        
		//gf1.set
		// 添加监听器
		//gf1.setOnClickListener(this);
		// 设置显示的大小，拉伸或者压缩
		//gf1.setShowDimension(600, 600);
		// 设置加载方式：先加载后显示、边加载边显示、只显示第一帧再显示
		//gf1.setGifImageType(GifImageType.COVER);
		
		//mainTab=(RadioGroup)findViewById(R.id.main_tab);  
	    //mainTab.setOnCheckedChangeListener(this);  
	        
		//this.mHost = getTabHost();
		//mHot = new Intent(this, IHotBlogListActivity.class);  
	   // mHost.addTab(mHost.newTabSpec("iHot").setIndicator(getResources().getString(R.string.ihosttag)).setContent(mHot));

//	    mTalk = new Intent(this, ListActivity.class);  
//	    mHost.addTab(mHost.newTabSpec("iTalk").setIndicator(getResources().getString(R.string.italktag)).setContent(mTalk));
//	    
//	    mRecommend = new Intent(this, ListActivity.class);  
//	    mHost.addTab(mHost.newTabSpec("iRecommend").setIndicator(getResources().getString(R.string.irecommend)).setContent(mRecommend));
//	    
//	    mAttention = new Intent(this, ListActivity.class);  
//	    mHost.addTab(mHost.newTabSpec("iAttention").setIndicator(getResources().getString(R.string.iattention)).setContent(mAttention));
//	    
//	    mMyself = new Intent(this, ListActivity.class);  
//	    mHost.addTab(mHost.newTabSpec("iMyself").setIndicator(getResources().getString(R.string.imyself)).setContent(mMyself));
//	    
	    
	    //mHost.setCurrentTab(0);
		////textview2 = ((TextView)findViewById(R.layout.firstopen_layout));
		//button2 = ((Button)findViewById(R.id.first_open_layout_button1));
		//list = (ListView) findViewById(R.id.appshowlistview);  
		//imageloader = new AsynImageLoader();
		//IClass.classapi.GetFirtShowData();

		//iAskListAdapter listItemAdapter = new iAskListAdapter(this,IClass.getappshowlist());
				
		

		//list.setAdapter(listItemAdapter); 

//		button2.setOnClickListener(new OnClickListener()
//		{
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				
//				
//				
//			}
//			
//		});
		
		
        
	}

	public IAskActivity() {
		// TODO Auto-generated constructor stub
	}


	
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
//		switch(checkedId){  
//        case R.id.radio_button0:  
//            this.mHost.setCurrentTabByTag("iHot");  
//            break;  
//        case R.id.radio_button1:  
//            this.mHost.setCurrentTabByTag("iTalk");  
//            break;  
//        case R.id.radio_button2:  
//            this.mHost.setCurrentTabByTag("iRecommend");  
//            break;  
//        case R.id.radio_button3:  
//            this.mHost.setCurrentTabByTag("iAttention");  
//            break;  
//        case R.id.radio_button4:  
//            this.mHost.setCurrentTabByTag("iMyself");  
//            break;   
//        }  
	}
	

}
